package com.erp.view;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.PointF;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;

import androidx.annotation.Nullable;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2018/3/23/023.
 * 作者：刘洋
 */

public class MyIvView extends View {
    public List<Bitmap> bitmaps=new ArrayList<>();
    public List<Integer> hs=new ArrayList<>();
    public boolean istu = false;
    public int mwidth=0;
    public int mheight;
    public MyIvView(Context context) {
        super(context);
    }

    public MyIvView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }

    public MyIvView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }
    public void setbitmap(Bitmap bitmap,int h) {
        bitmaps.add(bitmap);
        hs.add(h);
        istu = true;
        int zh=50;
        for(int i=0;i<hs.size();i++){
            zh=zh+hs.get(i)+50;
        }
        if(hs.size()>1) {
            mmatrix = new Matrix();
            float suo = (float) mheight /(float) zh;
            Log.d("TAAAG","w="+mwidth+"   h="+mheight+"   suo="+suo+"    zh="+zh);
            mmatrix.postScale(suo,suo, mwidth / 2, zh / 2);
            if(zh>mheight) {
                mmatrix.postTranslate(0,-((float) zh - (float) mheight) / 2);
            }
        }
        invalidate();
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        canvas.drawColor(Color.BLACK);
        canvas.setMatrix(mmatrix);
        if (istu) {
            Paint paint = new Paint();
            int h = 50;
            for (int i = 0; i < bitmaps.size(); i++) {
                if (i != 0) {
                    h = h + hs.get(i-1)+50;
                }
                canvas.drawBitmap(bitmaps.get(i), 50, h, paint);
            }
        }
    }

    private Matrix mmatrix=new Matrix();
    private PointF point1 = new PointF();
    /**
     * 区分移动还是缩放
     */
    private int mode = 0;// 初始状态
    /**
     * 拖拉模式
     */
    private int MODE_DRAG = 1;
    /**
     * 放大缩小模式
     */
    private int MODE_ZOOM = 2;
    /**
     * 用于记录开始时候的坐标位置
     */
    private PointF startPoint = new PointF();
    /**
     * 两个手指的开始距离
     */
    private float startDis = 0;
    /**
     * 两个手指的中间点
     */
    private PointF midPoint = new PointF();
    /**
     * 现在缩放的倍数
     */
    private float suofang = 1f;
    private float ddx = 0;
    private float ddy = 0;

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        Log.d("TAAAG","onTouch");
        float[] values = new float[9];
        mmatrix.getValues(values);
        point1 = new PointF((event.getX() - values[2]) / values[0], (event.getY() - values[5]) / values[0]);
        Log.d("TAAAG", "sss=" + point1.toString());
        ddx = values[2];
        ddy = values[5];
        suofang = values[0];
        switch (event.getAction() & MotionEvent.ACTION_MASK) {
            case MotionEvent.ACTION_DOWN: {
                startPoint.set(event.getRawX(), event.getRawY());
                mode = MODE_DRAG;
            }
            break;
            case MotionEvent.ACTION_MOVE:
                if (mode == MODE_DRAG) {
                    float dx = event.getRawX() - startPoint.x; // 得到x轴的移动距离
                    float dy = event.getRawY() - startPoint.y; // 得到x轴的移动距离
                    Log.d("TAAAG","X="+event.getRawX()+"    y="+event.getRawY()+"   X1="+startPoint.x+"   y1="+startPoint.y);
                    mmatrix.postTranslate(dx, dy);
                } else if (mode == MODE_ZOOM) {
                    float endDis = distance(event);// 结束距离
                    if (endDis > 10f) { // 两个手指并拢在一起的时候像素大于10
                        float scale = endDis / startDis;// 得到缩放倍数
                        startDis = endDis;
                        if (scale > 1) {
                            if (suofang < 10) {
                                mmatrix.postScale(scale, scale, midPoint.x, midPoint.y);
                            }
                        } else {
                            if (suofang > 0.1) {
                                mmatrix.postScale(scale, scale, midPoint.x, midPoint.y);
                            }
                        }
                    }
                }
            startPoint.set(event.getRawX(), event.getRawY());
            break;
            case MotionEvent.ACTION_POINTER_DOWN: {
                mode = MODE_ZOOM;
                /** 计算两个手指间的距离  */
                startDis = distance(event);
                /** 计算两个手指间的中间点  */
                if (startDis > 10f) { // 两个手指并拢在一起的时候像素大于10
                    midPoint = mid(event);
                }
            }
        }
        invalidate();
        return true;

    }

    /**
     * 计算两个手指间的中间点
     */
    private PointF mid(MotionEvent event) {
        float midX = (event.getX(1) + event.getX(0)) / 2;
        float midY = (event.getY(1) + event.getY(0)) / 2;
        return new PointF(midX, midY);
    }

    /**
     * 计算两个手指间的距离
     */
    private Float distance(MotionEvent event) {
        float dx = 0f;
        float dy = 0f;
        try {
            dx = event.getX(1) - event.getX(0);
            dy = event.getY(1) - event.getY(0);
        } catch (IllegalArgumentException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        /** 使用勾股定理返回两点之间的距离  */
        return (float) Math.sqrt(dx * dx + dy * dy);
    }

}
